# -*- coding:utf-8 -*-
import json
import random
import time
from ssl import _create_unverified_context
from json import loads

import jieba.analyse as analyse
import pyperclip as pyperclip
from lxml import etree
from selenium import webdriver
from selenium.webdriver import Keys
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import tkinter.messagebox

import urllib.request
import urllib.parse

storeProductJ = '{"image":"http://127.0.0.1:8080/crmebimage/public/maintain/2021/12/25/b73c648fb7fd45cf8782e4ca0bc28a42s2a8dpad2x.png","sliderImages":["http://127.0.0.1:8080/crmebimage/public/maintain/2021/12/25/9e97ddf78f804d50b985bdcf666478ebj76hbf3gpp.jpg","http://127.0.0.1:8080/crmebimage/public/maintain/2021/12/25/b9f86161d09a44d29f7e5083d402245321764lzsus.jpg"],"videoLink":"","sliderImage":"[\\"http://127.0.0.1:8080/crmebimage/public/maintain/2021/12/25/9e97ddf78f804d50b985bdcf666478ebj76hbf3gpp.jpg\\",\\"http://127.0.0.1:8080/crmebimage/public/maintain/2021/12/25/b9f86161d09a44d29f7e5083d402245321764lzsus.jpg\\"]","storeName":"123","storeInfo":"123","keyword":"123 123","cateIds":[246,248,258,488],"cateId":"246,248,258,488","unitName":"123","sort":0,"giveIntegral":0,"ficti":0,"isShow":false,"isBenefit":false,"isNew":true,"isGood":false,"isHot":true,"isBest":false,"tempId":1,"attrValue":[{"image":"http://127.0.0.1:8080/crmebimage/public/maintain/2021/12/25/b73c648fb7fd45cf8782e4ca0bc28a42s2a8dpad2x.png","price":0,"cost":0,"otPrice":0,"stock":0,"barCode":"","weight":0,"volume":0,"attrValue":"{\\"规格\\":\\"默认\\"}"}],"attr":[{"attrName":"规格","attrValues":"默认","id":0}],"selectRule":"","isSub":false,"content":"<p>12356</p>","specType":false,"id":0,"couponIds":[],"coupons":[],"activity":["默认","秒杀","砍价","拼团"]}'


# typeId, movie_count, rating, vote_count,start
def get_url_data_in_ranking_list2(page=2):
    storeContent='<div data-v-8c7b5918="" class="text-content"><i class="img-wrapper" style="padding-bottom:166%"><img src="https://t11.baidu.com/it/u=2446673225,698559744&amp;fm=199&amp;app=68&amp;f=JPEG?w=750&amp;h=1245&amp;s=0F547A861E5717E740BC05330300C042" loading="lazy" class="richtext-img" style="background: none;"></i><br><i class="img-wrapper" style="padding-bottom:100%"><img src="https://t10.baidu.com/it/u=3373800018,3037034019&amp;fm=199&amp;app=68&amp;f=JPEG?w=750&amp;h=750&amp;s=1D966C9652C273E75702D1240300E06A" loading="lazy" class="richtext-img" style="background: none;"></i><br><i class="img-wrapper" style="padding-bottom:100%"><img src="https://t10.baidu.com/it/u=3088398516,3269224923&amp;fm=199&amp;app=68&amp;f=JPEG?w=750&amp;h=750&amp;s=15826EB3528E1FE71F1428EC0300E072" loading="lazy" class="richtext-img" style="background: none;"></i><br><i class="img-wrapper" style="padding-bottom:100%"><img src="https://t12.baidu.com/it/u=30265435,2181381253&amp;fm=199&amp;app=68&amp;f=JPEG?w=750&amp;h=750&amp;s=030268A37A5A3FE74434442D03003042" loading="lazy" class="richtext-img" style="background: none;"></i><br><i class="img-wrapper" style="padding-bottom:100%"><img src="https://t10.baidu.com/it/u=1230766845,65539484&amp;fm=199&amp;app=68&amp;f=JPEG?w=750&amp;h=750&amp;s=9194789350C176F441B2403403001062" loading="lazy" class="richtext-img" style="background: none;"></i><br><i class="img-wrapper" style="padding-bottom:100%"><img src="https://t11.baidu.com/it/u=2880205970,733118762&amp;fm=199&amp;app=68&amp;f=JPEG?w=750&amp;h=750&amp;s=4B926887560307E71818647C0300A052" loading="lazy" class="richtext-img" style="background: none;"></i><br><i class="img-wrapper" style="padding-bottom:133.33333333333334%"><img src="https://t12.baidu.com/it/u=1215037053,534467661&amp;fm=199&amp;app=68&amp;f=JPEG?w=750&amp;h=1000&amp;s=2B147B870DDEBBED314DF00E03003051" loading="lazy" class="richtext-img" style="background: none;"></i><br><i class="img-wrapper" style="padding-bottom:105.73333333333333%"><img src="https://t11.baidu.com/it/u=1291156005,2264008229&amp;fm=199&amp;app=68&amp;f=JPEG?w=750&amp;h=793&amp;s=F1EABA57508EF6ED2A71895D03008063" loading="lazy" class="richtext-img" style="background: none;"></i></div>'
    try:
        context = _create_unverified_context()  # 屏蔽ssl证书
        headers = {
            'User-Agent': 'Mozilla/5.0 (X11; Linux aarch64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.188 Safari/537.36 CrKey/1.54.250320',
            'Referer': 'https://b2b.baidu.com/ss?q=%E9%A2%84%E5%88%B6%E8%8F%9C&from=b2b_straight&srcid=5103&from_restype=product&pi=baidu.b2b_straight.title'
        }
        page=str(page)
        param=f'ajax=1&csrf_token=8daa38c03bf9573cd56db18b58ea8e0f&logid=3198597403530612833&fid=0%2C1685703478729&_=1685703526892&q=%E9%A2%84%E5%88%B6%E8%8F%9C&from=b2b_straight&srcid=5103&from_restype=product&pi=baidu.b2b_straight.title&o=0&p={page}&mk=%E5%85%A8%E9%83%A8%E7%BB%93%E6%9E%9C&f=[]&s=30&adn=0&resType=product&fn=%7B%22brand_name%22:%22%E5%93%81%E7%89%8C%22,%22select_param3%22:%22%E5%8F%A3%E5%91%B3%22,%22select_param1%22:%22%E5%82%A8%E8%97%8F%E6%96%B9%E6%B3%95%22,%22select_param2%22:%22%E4%BA%A7%E5%93%81%E7%B1%BB%E5%88%AB%22%7D'
        url = 'https://b2b.baidu.com/s/a?' + param
        # print(url)
        req = urllib.request.Request(url=url, headers=headers)
        f = urllib.request.urlopen(req, context=context)
        response = f.read().decode("utf-8")
        jsonData = loads(response)  # 将json转为python对象

        storeProductL = list()


        # print(storeProduct)
        for product in jsonData['data']['productList']:
            storeProduct = loads(storeProductJ)
            # print(type(product))
            info = product['fullName']
            price = product['price']
            image = product['picUrl']
            name = product['fullName']
            # print('z')
            storeProduct['image'] = image
            storeProduct['storeName'] = name
            storeProduct['unitName'] = "百度爱采购爬取数据" #name
            storeProduct["attrValue"][0]["image"] = image
            storeProduct["attrValue"][0]["price"] = price
            storeProduct["attrValue"][0]["otPrice"] = price
            # print("y")
            keywords = analyse.extract_tags(info, topK=3, withWeight=True, allowPOS=())
            storeProduct["sliderImages"] = [image]
            storeProduct["sliderImage"] = "[" + image+","+image + "]"
            storeProduct["keyword"] = ""
            for tuple in keywords:
                storeProduct["keyword"] = storeProduct["keyword"] + " " + tuple[0]
            storeProduct["storeInfo"] = info
            storeProduct['content']=storeContent

            # print(storeProduct)
            storeProductL.append(storeProduct)

        return storeProductL

    except Exception as ex:
        print(ex)
        err_str = "出现未知异常：{}".format(ex)
        return [err_str, -1]


#总共267个item food
for x in range(10):
    paper=x+2
    try:
        storeProductL=get_url_data_in_ranking_list2(paper)
        # 将美食信息写入JSON文件
        with open('../foodJsonData/foodStoreData'+str(x)+'.json', 'w', encoding='utf-8') as f:
            json.dump(storeProductL, f, ensure_ascii=False, indent=4)
    except Exception as ex:
        print("爬取完了......")
        print(ex)


